package com.collect.cameroon.zhiScene.zhiPhoneReview.zhiFollow;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.collect.cameroon.MyApplication;
import com.collect.cameroon.R;
import com.collect.cameroon.zhiAdater.zhiPhoneReview.ZHIPhoneReviewContactAdjuster;
import com.collect.cameroon.zhiAdater.zhiPhoneReview.ZHIPhoneReviewFollowAdjuster;
import com.collect.cameroon.zhiBase.BaseFragment;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRPhoneReviewListEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISPhoneReviewFollowEntity;
import com.collect.cameroon.zhiBean.zhiSend.ZHISPhoneReviewMarkedEntity;
import com.collect.cameroon.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.cameroon.zhiHelper.ZHIMainProcessHelper;
import com.collect.cameroon.zhiScene.zhiPhoneReview.zhiRoot.ZHISPhoneReviewMarkedImpl;
import com.collect.cameroon.zhiScene.zhiPhoneReview.zhiRoot.ZHIRootPhoneReviewFragmentInterface;
import com.collect.cameroon.zhiScene.zhiWidget.zhiDialog.ZHIDialogMessage;
import com.collect.cameroon.zhiScene.zhiWidget.zhiMenuView.ZHIMenuTableView;
import com.collect.cameroon.zhiScene.zhiWidget.zhiStepView.ZHINextStepView;
import com.collect.cameroon.zhiUtils.StringUtil;
import com.collect.cameroon.zhiUtils.ToastUtil;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class ZHIPhoneReviewFollowFragment extends BaseFragment<ZHIPhoneReviewFollowPresenter> implements ZHIPhoneReviewFollowContract.View {

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.iv_filter)
    ImageView iv_filter;
    @BindView(R.id.iv_products)
    ImageView iv_products;

    @BindView(R.id.tv_total_num)
    TextView tv_total_num;
    @BindView(R.id.tv_remaind_num)
    TextView tv_remaind_num;

    @BindView(R.id.rc_contact_list)
    RecyclerView rc_contact_list;

    @BindView(R.id.mtv_full_name)
    ZHIMenuTableView mtv_full_name;
    @BindView(R.id.mtv_gender)
    ZHIMenuTableView mtv_gender;
    @BindView(R.id.mtv_birthday)
    ZHIMenuTableView mtv_birthday;
    @BindView(R.id.mtv_marital_status)
    ZHIMenuTableView mtv_marital_status;
    @BindView(R.id.mtv_email)
    ZHIMenuTableView mtv_email;
    @BindView(R.id.mtv_bvn)
    ZHIMenuTableView mtv_bvn;
    @BindView(R.id.mtv_job_type)
    ZHIMenuTableView mtv_job_type;
    @BindView(R.id.mtv_company_name)
    ZHIMenuTableView mtv_company_name;
    @BindView(R.id.mtv_start_working_there)
    ZHIMenuTableView mtv_start_working_there;
    @BindView(R.id.mtv_monthly_income)
    ZHIMenuTableView mtv_monthly_income;
    @BindView(R.id.mtv_product)
    ZHIMenuTableView mtv_product;

    @BindView(R.id.rc_follow_list)
    RecyclerView rc_follow_list;

    @BindView(R.id.cb_pass)
    CheckBox cb_pass;
    @BindView(R.id.cb_reject)
    CheckBox cb_reject;
    @BindView(R.id.cb_mark)
    CheckBox cb_mark;

    @BindView(R.id.et_remark)
    EditText et_remark;

    @BindView(R.id.btn_negtive)
    ZHINextStepView btn_negtive;

    private String mOrderId;
    private String mStatus;

    private ZHIRootPhoneReviewFragmentInterface mZHIRootPhoneReviewFragmentInterface;

    private final ZHIPhoneReviewContactAdjuster mZHIPhoneReviewContactAdjuster = new ZHIPhoneReviewContactAdjuster();

    private final ZHIPhoneReviewFollowAdjuster mZHIPhoneReviewFollowAdjuster = new ZHIPhoneReviewFollowAdjuster();

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_phone_review_follow;
    }

    @Override
    protected void initView(View view) {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));
            srl_swipe_layout.setOnRefreshListener(() -> initData(false));

            iv_filter.setOnClickListener(v -> showFilter());

            iv_products.setOnClickListener(v -> ZHIDialogMessage.showDialog(getActivity(),
                    getResources().getString(R.string.prompt),
                    getResources().getString(R.string.logout_confirm),
                    getResources().getString(R.string.cancel),
                    getResources().getString(R.string.confirm),
                    tag -> {
                        if (1 == tag) {
                            //退出
                            ZHIMainProcessHelper.getInstance().logout();
                            ZHIProjectRouter.toRegLoginV2();
                        }
                    }));

            rc_contact_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
            rc_contact_list.setAdapter(mZHIPhoneReviewContactAdjuster);

            rc_follow_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
            rc_follow_list.setAdapter(mZHIPhoneReviewFollowAdjuster);

            cb_pass.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if(buttonView.equals(cb_pass)){
                    if(isChecked){
                        cb_mark.setChecked(false);
                        cb_reject.setChecked(false);
                    }
                }
            });

            cb_reject.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if(buttonView.equals(cb_reject)){
                    if(isChecked){
                        cb_mark.setChecked(false);
                        cb_pass.setChecked(false);
                    }
                }
            });

            cb_mark.setOnCheckedChangeListener((buttonView, isChecked) -> {
                if(buttonView.equals(cb_mark)){
                    if(isChecked){
                        cb_reject.setChecked(false);
                        cb_pass.setChecked(false);
                    }
                }
            });

            btn_negtive.setOnMenuItemDidChangedListener(item -> {
                try {
                    if (checkCanCommit()) {
                        if (mPresenter != null) {
                            String remark = StringUtil.safeString(et_remark.getText().toString().trim());

                            if (TextUtils.isEmpty(mOrderId)) {
                                return;
                            }
                            if (TextUtils.isEmpty(mStatus)) {
                                return;
                            }

                            ZHISPhoneReviewMarkedEntity zhisPhoneReviewMarkedEntity = new ZHISPhoneReviewMarkedEntity();
                            zhisPhoneReviewMarkedEntity.setContent(remark);
                            zhisPhoneReviewMarkedEntity.setDsStatus(mStatus);
                            zhisPhoneReviewMarkedEntity.setOrderId(mOrderId);

                            mPresenter.markPhoneReview(getActivity(), zhisPhoneReviewMarkedEntity);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            mZHIRootPhoneReviewFragmentInterface = (ZHIRootPhoneReviewFragmentInterface) activity;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        initData(true);
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @Override
    public void onGetPhoneReviewList(ZHIRPhoneReviewListEntity zhirPhoneReviewListEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            tv_total_num.setText(zhirPhoneReviewListEntity.getTodayData().getTodayTotalNum());
            tv_remaind_num.setText(zhirPhoneReviewListEntity.getTodayData().getTodayRemainNum());

            mOrderId = zhirPhoneReviewListEntity.getOrderId();

            mZHIPhoneReviewContactAdjuster.setNewData(zhirPhoneReviewListEntity.getContactData());

            mtv_full_name.setDetailText(zhirPhoneReviewListEntity.getUserData().getFullName());
            mtv_gender.setDetailText(zhirPhoneReviewListEntity.getUserData().getGender());

            mtv_birthday.setDetailText(zhirPhoneReviewListEntity.getUserData().getBirthDate());
            mtv_marital_status.setDetailText(zhirPhoneReviewListEntity.getUserData().getMaritalStatus());

            mtv_email.setDetailText(zhirPhoneReviewListEntity.getUserData().getEmail());
            mtv_bvn.setDetailText(zhirPhoneReviewListEntity.getUserData().getBvn());

            mtv_job_type.setDetailText(zhirPhoneReviewListEntity.getUserData().getEmploymentType());
            mtv_company_name.setDetailText(zhirPhoneReviewListEntity.getUserData().getCompanyName());

            mtv_start_working_there.setDetailText(zhirPhoneReviewListEntity.getUserData().getWorkingSince());
            mtv_monthly_income.setDetailText(zhirPhoneReviewListEntity.getUserData().getMonthlyIncome());

            mtv_product.setDetailText(zhirPhoneReviewListEntity.getUserData().getProduct());

            mZHIPhoneReviewFollowAdjuster.setNewData(zhirPhoneReviewListEntity.getFollowData());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onMarkPhoneReview() {
        try {
            srl_swipe_layout.setRefreshing(false);
            resetMarkUI();
            initData(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initData(boolean showLoading) {
        if (mPresenter != null) {
            ZHISPhoneReviewFollowEntity zhisPhoneReviewFollowEntity = getCaseSearchParams();
            if (zhisPhoneReviewFollowEntity == null) {
                srl_swipe_layout.setRefreshing(false);
                return;
            }
            mPresenter.getPhoneReviewList(getActivity(), zhisPhoneReviewFollowEntity, showLoading);
        }
    }

    private void resetMarkUI(){
        try {
            cb_pass.setChecked(false);
            cb_mark.setChecked(false);
            cb_reject.setChecked(false);
            et_remark.setText(null);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void refreshData() {
        initData(true);
    }

    public void showFilter() {
        if (mZHIRootPhoneReviewFragmentInterface != null) {
            mZHIRootPhoneReviewFragmentInterface.onShowFilterFollow();
        }
    }

    public ZHISPhoneReviewFollowEntity getCaseSearchParams() {
        if (mZHIRootPhoneReviewFragmentInterface != null) {
            return mZHIRootPhoneReviewFragmentInterface.onGetFollowFilterParams();
        }
        return new ZHISPhoneReviewFollowEntity();
    }

    private boolean checkCanCommit() {
        try {
            mStatus = "";
            String remark = StringUtil.safeString(et_remark.getText().toString().trim());

            if (TextUtils.isEmpty(mOrderId)) {
                ToastUtil.s(MyApplication.getAppContext().getString(R.string.user_does_not_exist));
                return false;
            }

            if (cb_reject.isChecked()) {
                if (TextUtils.isEmpty(remark)) {
                    ToastUtil.s(MyApplication.getAppContext().getString(R.string.pls_enter_follow_content));
                    return false;
                }
                mStatus = ZHISPhoneReviewMarkedImpl.DS_DENIED;
                return true;
            }

            if (cb_mark.isChecked()) {
                if (TextUtils.isEmpty(remark)) {
                    ToastUtil.s(MyApplication.getAppContext().getString(R.string.pls_enter_follow_content));
                    return false;
                }
                mStatus = ZHISPhoneReviewMarkedImpl.DS_SUSPEND;
                return true;
            }

            if (cb_pass.isChecked()) {
                mStatus = ZHISPhoneReviewMarkedImpl.DS_APROVAL;
                return true;
            }

            ToastUtil.s(MyApplication.getAppContext().getString(R.string.pls_select_the_follow_status));

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

}
